// 引入 VueRouter 插件
import VueRouter from "vue-router";

// 获取原型对象push函数
const originalPush = VueRouter.prototype.push
// 获取原型对象replace函数
const originalReplace = VueRouter.prototype.replace
// 修改原型对象中的push函数
VueRouter.prototype.push = function push(location) {
    return originalPush.call(this, location).catch(err => err)
}
// 修改原型对象中的replace函数
VueRouter.prototype.replace = function replace(location) {
    return originalReplace.call(this, location).catch(err => err)
}

const routes = [
    {
    path: '/',
        // 当访问根路径时重定向到登陆页面
        // 1.重定向 redirect: '/login'
        // 2.组件路由 component: login
        redirect: '/login'
    },
    {
        path: '/login',
        component: () => import('@/page/login/index'),
    },
    {
        name: "home",
        path: "/home",
        component: () => import('@/page/home'),
        children: [
            {
                name: "main",
                path: "/main",
                component: () => import('@/page/main')
            },
            {
                name: "vColorPicker",
                path: '/vColorPicker',
                component: () => import('@/page/component/vColorPicker')
            },
            {
                name: "vDragSort",
                path: '/vDragSort',
                component: () => import('@/page/component/vDragSort')
            },
            {
                name: "vVirtualList",
                path: '/vVirtualList',
                component: () => import('@/page/component/vVirtualList')
            },
            {
                name: "vInputSearch",
                path: '/vInputSearch',
                component: () => import('@/page/component/vInputSearch')
            },
            {
                name: "vModal",
                path: '/vModal',
                component: () => import('@/page/component/vModal')
            },
            {
                name: "vOrgPersonTree",
                path: '/vOrgPersonTree',
                component: () => import('@/page/component/vOrgPersonTree')
            },
            {
                name: "vPersonTree",
                path: '/vPersonTree',
                component: () => import('@/page/component/vPersonTree')
            },
            {
                name: "map",
                path: '/map',
                component: () => import('@/page/example/map/index')
            },
            {
                name: "copyText",
                path: '/copyText',
                component: () => import('@/page/example/copyText/index')
            },
            {
                name: "flipClock",
                path: '/flipClock',
                component: () => import('@/page/example/flipClock/index')
            },
            {
                name: "process",
                path: '/process',
                component: () => import('@/page/example/process/index')
            }
        ]
    },
]


const router = new VueRouter({
    routes
})

// 挂载路由导航守卫
router.beforeEach((to, from, next) => {
    // to 将要访问的路径
    // from 从哪个路径跳转而来
    // next 是一个函数，表示放行
    //  1.next() 放行
    //  2.next('/login') 强制跳转
    if (to.path === "/login") return next();
    // 获取 token
    const tokenStr = window.localStorage.getItem("token");
    if (!tokenStr) return next("/login");
    next();
});

// 暴露路由
export default router
